package com.ashley.apptools.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDrag extends GridView {

	public GridViewDrag(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public GridViewDrag(Context arg0) {
		// TODO Auto-generated constructor stub
		super(arg0);
	}
	public GridViewDrag(AttributeSet arg1, int arg2, Context arg0) {
		// TODO Auto-generated constructor stub
		super(arg0, arg1, arg2);
	}
	
	private int							dragPosition;
	private int							dragPointX;
	private int							dragPointY;
	private int							dragOffsetX;
	private int							dragOffsetY;
	
	private ImageView					dragImageView;		
	private WindowManager				windowManager;
	private WindowManager.LayoutParams	windowParams;
	private DropListener listener;
    

	private void setOnItemLongClickListener(final MotionEvent event ,int vx,int vy)
	{
		
		final int x = vx;
		final int y=vy;
		this.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				
				dragPosition = arg2;
				ViewGroup itemGroup = (ViewGroup) getChildAt(dragPosition-getFirstVisiblePosition()); 
				dragPointX = x-itemGroup.getLeft();
				dragPointY = y-itemGroup.getTop();
				
				dragOffsetX = (int) (event.getRawX()-x);
				dragOffsetY = (int) (event.getRawY()-y);
				
				
				itemGroup.destroyDrawingCache();
				itemGroup.setDrawingCacheEnabled(true);
				Bitmap bitmap = Bitmap.createBitmap(itemGroup.getDrawingCache());
				startDrag(bitmap, x, y+5);
				return true;
			}
		});
	}
	protected void startDrag(Bitmap bitmap,int vx,int vy)
	{
		stopDrag();
		windowParams = new WindowManager.LayoutParams();
		windowParams.gravity = Gravity.TOP|Gravity.LEFT;
		windowParams.x = vx - dragPointX+dragOffsetX;
		windowParams.y = vy - dragPointY+dragOffsetY;
		
		windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		
		windowParams.format = PixelFormat.TRANSLUCENT;
		windowParams.windowAnimations = 0;
		
		dragImageView = new ImageView(getContext());
		dragImageView.setImageBitmap(bitmap);
		if(windowManager == null)
		{
			windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
		}
		windowManager.addView(dragImageView, windowParams);
	}
	protected void stopDrag()
	{
		if (dragImageView != null)
		{
			windowManager.removeView(dragImageView);
			dragImageView = null;
		}
	}
	protected void onDrag(int vx,int vy)
	{
		if (dragImageView != null)
		{
			windowParams.alpha = 0.6f;
			windowParams.x = vx - dragPointX + dragOffsetX;
			windowParams.y = vy - dragPointY + dragOffsetY;
			windowManager.updateViewLayout(dragImageView, windowParams);
			
		}
		if (listener !=null) {
			listener.onDrag(dragPosition, vx, vy);
		}
	}
	protected void onDrop(int vx,int vy)
	{
		if(listener != null)
		{
			listener.onDrop(dragPosition, vx, vy);
		}
	}
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(dragImageView !=null&&dragPosition != AdapterView.INVALID_POSITION)
		{
			int x = (int) ev.getX();
			int y = (int) ev.getY();
			switch (ev.getAction()) {
			case MotionEvent.ACTION_MOVE:
				onDrag(x, y+5);
				break;
			case MotionEvent.ACTION_UP:
				stopDrag();
				onDrop(x, y+5);
				setEnabled(true);
				break;
			}
			return true;
		}
		return super.onTouchEvent(ev);
	}
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if(ev.getAction() == MotionEvent.ACTION_DOWN)
		{
			int x = (int) ev.getX();
			int y = (int) ev.getY();
			setOnItemLongClickListener(ev, x, y);
			return true;
		}
			
		
		return super.onInterceptTouchEvent(ev);
	}
	public interface DropListener
	{
		void onDrag(int dragPosition,int vx,int vy);
		void onDrop(int dragPosition,int vx,int vy);
		
	}
	public void setDropListener(DropListener listener)
	{
		this.listener = listener;
		
	}
	public boolean isDragTarget(View view)
	{
		if(view !=null)
		{
			int[] location = new int[2];
			view.getLocationInWindow(location);
			int x = windowParams.x;
			int y = windowParams.y;
			int vx = location[0];
			int vy = location[1];
			
			int width = view.getWidth();
			int height = view.getHeight();
			if(vx <x &&x<vx+width &&vy<y &&y <vy +height)
			{
				return true;
			}
		}
		return false;
	}
	
}
